package com.space.school.dao.impl;

import com.space.school.dao.UserDao;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class UserDaoImpl implements UserDao {
//    获取数据库并连接
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(SpaceJDBCUtils.getDs());
    @Override
    public List<User> search(int start, int pageSize,Map<String,String[]> map) {
//        String sql = "select userId,userName,passWord, case when status=1 then '生效' else '失效' end as status,createTime,updateTime from user";
//        String sql = "select * from user order by UserId limit ?,?";
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("select * from user where 1=1  ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
//        取map中的键的数组
        Set<String> keyset=map.keySet();
        for (String key:keyset) {
            if(!map.get(key)[0].equals("") && !key.equals("currentPage")){
                stringBuffer.append(" and " + key + " like ?");
                params.add("%"+map.get(key)[0]+"%");
            }
        }
        params.add(start);
        params.add(pageSize);
        stringBuffer.append(" order by userId  limit ?,? ");
        return jdbcTemplate.query(stringBuffer.toString(), new BeanPropertyRowMapper<User>(User.class),params.toArray());
    }

    @Override
    public int count(Map<String,String[]> map) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("select count(*) from user where 1=1  ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
//        取map中的键的数组
        Set<String> keyset=map.keySet();
        for (String key:keyset) {
            if(!map.get(key)[0].equals("")&& !key.equals("currentPage")){
                stringBuffer.append(" and " + key + " like ?");
                params.add("%"+map.get(key)[0]+"%");
            }
        }
        return jdbcTemplate.queryForObject(stringBuffer.toString(),Integer.class,params.toArray());
    }

    @Override
    public int delete(int id) {
        // 登录查询
        String sql="delete from user where userId=?";
        int result=jdbcTemplate.update(sql,id);
        return result;
    }

    @Override
    public void update(User user) {
        // 更新语句
        String sql="UPDATE user SET password = ?,updateTime=? WHERE userId = ? ";
        jdbcTemplate.update(sql,user.getPassWord(),new Date(),user.getUserId());
    }

    @Override
    public User findById(int id) {
        String sql = "select * from user where userId=?";
        List<User> users=jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class),id);
        if(users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public void updateStatus(int id,String status) {
        // 更新语句
        String sql="UPDATE user SET status = ?,updateTime=? WHERE userId = ? ";
        jdbcTemplate.update(sql,status,new Date(),id);
    }

    @Override
    public void save(User user) {
        // 新增语句
        String sql="insert into user (username,password,createTime) values(?,?,?)";
        jdbcTemplate.update(sql,user.getUserName(),user.getPassWord(),new Date());
    }

    @Override
    public boolean existUsername(String username) {
        String sql = "select * from user where userName=?";
        List<User> users=jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class),username);
        if(users.size()>0){
            return false;
        }
        return true;
    }
}
